import Animation from '../base/animation'
import DataBus from '../databus'

const GIFTS_IMG_SRC = [
  'images/gift1.png', 'images/gift2.png',
  'images/gift3.png', 'images/gift4.png',
  'images/gift5.png','images/gift6.png'
]
const GIFT_WIDTH = 60
const GIFT_HEIGHT = 60

let GIFT_IMG_SRC = ''
const __ = {
  speed: Symbol('speed')
}

let databus = new DataBus()

function rnd(start, end) {
  return Math.floor(Math.random() * (end - start) + start)
}

function rndBg() {
  let rd = Math.floor(Math.random() * 6)
  return GIFTS_IMG_SRC[rd]
}

export default class Gifts extends Animation {
  constructor() {
    super(GIFT_IMG_SRC, GIFT_WIDTH, GIFT_HEIGHT)

  }

  init(speed) {
    GIFT_IMG_SRC = rndBg()
    this.x = rnd(0, window.innerWidth - GIFT_WIDTH)
    this.y = -this.height
    this[__.speed] = speed
    this.visible = true
    this.src = GIFT_IMG_SRC
    
    switch (GIFT_IMG_SRC){
      case 'images/gift3.png':
        this.type = 'small'
        break;
      case 'images/gift5.png':
        this.type = 'money'
          break;
      default:
        this.type = 'gift'
        break;
    }
  }
  // 每一帧更新子弹位置
  update() {
    this.y += this[__.speed]
    // 对象回收
    if (this.y > window.innerHeight + this.height)
      databus.removeEnemey(this)
  }
}